package com.yyq.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyq.eduservice.entity.EduCourse;
import com.yyq.eduservice.entity.EduCourseDescription;
import com.yyq.eduservice.entity.frontVo.courseFrontVo;
import com.yyq.eduservice.entity.frontVo.courseInfoVo;
import com.yyq.eduservice.entity.vo.CourseInfo;
import com.yyq.eduservice.entity.vo.CourseInfoPublish;
import com.yyq.eduservice.mapper.EduCourseMapper;
import com.yyq.eduservice.service.EduChapterService;
import com.yyq.eduservice.service.EduCourseDescriptionService;
import com.yyq.eduservice.service.EduCourseService;
import com.yyq.eduservice.service.EduVideoService;
import com.yyq.servicebase.exceptionhandler.YyqException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yyq
 * @since 2021-04-15
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    //注入描述
    @Autowired
    private EduCourseDescriptionService descriptionService;
    //注入章节和小结
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private EduVideoService videoService;



    @Override
    public String addcourse(CourseInfo courseInfo) {
        //添加两张表　第一张Ｃｏｕｒｓｅ表
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfo,eduCourse);
        //eduCourse.setSubjectParentId("0");
        int insert = baseMapper.insert(eduCourse); //返回ｉｎｓｅｒｔ　　插入数据库的行数
        if(insert==0){
            throw new YyqException();
        }
        //注入ｃｏｕｒｓｅ＿ｄｅｓｃｒｅｔｉｏｎｓｅｒｖｉｃｅ
        EduCourseDescription description=new EduCourseDescription();
        description.setDescription(courseInfo.getDescription());
        description.setId(eduCourse.getId());
        descriptionService.save(description);

        return eduCourse.getId();
    }

    @Override
    //通过课程ｉｄ查询到课程信息
    public CourseInfo getCourseInfoById(String courseId) {
        //查询　ｃｏｕｒｓｅ表信息　并且封装到ＣＯＵＲＳＥｉｎｆｏ
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfo courseInfo=new CourseInfo();
        BeanUtils.copyProperties(eduCourse,courseInfo);
        //查询course_descrepttion 把信息传到ｃｏｕｒｓｅｉｎｆｏ
        EduCourseDescription description = descriptionService.getById(courseId);
        courseInfo.setDescription(description.getDescription());

        return courseInfo;
    }

    @Override
    public void updateCourseInfo(CourseInfo courseInfo) {
        //更改course
        EduCourse eduCourse=new EduCourse();
        //前面往后边注入数据
        BeanUtils.copyProperties(courseInfo,eduCourse);
        System.out.println(eduCourse);
        int i = baseMapper.updateById(eduCourse);

//        if(i==0){
//            throw new YyqException();
//        }

        //更改course_descreption
        EduCourseDescription description=new EduCourseDescription();
        description.setId(courseInfo.getId());
        description.setDescription(courseInfo.getDescription());
        descriptionService.updateById(description);
    }

    //
    @Override
    public CourseInfoPublish publisCourseInfo(String id) {

        CourseInfoPublish courseInfoPublish = baseMapper.courseInfoPublish(id);
        return courseInfoPublish;
    }

    @Override
    public void deleteAllInfo(String courseId) {
        //删除小节信息　同时删除云端视频
        //TODO　删除对应的视频
        videoService.deleteByCourseId(courseId);
        //删除章节信息
        chapterService.deleteByCourseId(courseId);
        //删除描述信息
        descriptionService.removeById(courseId);
        //删除课程信息
        baseMapper.deleteById(courseId);
    }

    @Override
    public Map getCourseFrontPage(Page<EduCourse> pageParam, courseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        wrapper.eq("status","Normal"); //已发布
        if (courseFrontVo.getSubjectParentId()!=null) {
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }

        if (courseFrontVo.getSubjectId()!=null) {
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }

        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }


        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    //获取前端课程页面详细信息
    @Override
    public courseInfoVo getCourseFrontInfo(String courseId) {
        return baseMapper.getCourseFrontInfo(courseId);
    }

    @Override
    public List<EduCourse> searchCourse(String search) {
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        wrapper.like("title",search);
        List<EduCourse> list = baseMapper.selectList(wrapper);
        return list;
    }

}
